//=============================================================================
//     _   _                               ____                _  _          
//    | | | |  __ _  _ __   _ __   _   _  / ___|  _ __   _ __ (_)| |_   ___ 
//    | |_| | / _` || '_ \ | '_ \ | | | | \___ \ | '_ \ | '__|| || __| / _ \
//    |  _  || (_| || |_) || |_) || |_| |  ___) || |_) || |   | || |_ |  __/
//    |_| |_| \__,_|| .__/ | .__/  \__, | |____/ | .__/ |_|   |_| \__| \___|
//                  |_|    |_|     |___/         |_|                         
//
//                     HappySprite - We make sprites happy
//
// Copyright (c) 2007 by Tank Monkey Games
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//=============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace HappySprite.Graphics
{
    public class ImageMap
    {
        public Texture2D Texture;
        public Vector2 UpperLeft;
        public Vector2 LowerRight;

        public ImageMap(Texture2D texture)
            : this(texture, Vector2.Zero, Vector2.One)
        {
        }

        public ImageMap(Texture2D texture, Vector2 upperLeft, Vector2 lowerRight)
        {
            Texture = texture;
            UpperLeft = upperLeft;
            LowerRight = lowerRight;
        }

        public ImageMap(string texture)
            : this(texture, Vector2.Zero, Vector2.One)
        {
        }

        public ImageMap(string texture, Vector2 upperLeft, Vector2 lowerRight)
        {
            Texture = Global.Content.Load<Texture2D>(texture);
            UpperLeft = upperLeft;
            LowerRight = lowerRight;
        }

        public ImageMap(Texture2D texture, int columns, int rows, int index)
        {
            Initialize(texture, columns, rows, index);
        }

        public ImageMap(string texture, int columns, int rows, int index)
        {
            Initialize(Global.Content.Load<Texture2D>(texture), columns, rows, index);
        }

        void Initialize(Texture2D texture, int columns, int rows, int index)
        {
            Texture = texture;

            int x1 = index % columns;
            int y1 = index / rows;
            int x2 = x1 + 1;
            int y2 = y1 + 1;

            UpperLeft = new Vector2((float)x1 / (float)columns, (float)y1 / (float)rows);
            LowerRight = new Vector2((float)x2 / (float)columns, (float)y2 / (float)rows);
        }
    }
}
